﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BLL.ComplexClasses;
using DAL;
using App_Log;
using App_Utils;

namespace BLL
{
    public class BL_Users
    {
        public static User GetUserbyLoginId(string login_id)
        {
            User objUser = new User();
            try
            {
                clsLog.logMsg("User", "GetUserbyLoginId", "Started");

                DAL.ithubEntities db = new ithubEntities();
                tblUser _objUser = (from q in db.tblUsers where q.login_id == login_id select q).SingleOrDefault();
                if (_objUser != null)
                {
                    objUser = (User)App_Utils.clsUtils.ConvertToObject(_objUser, objUser);
                }
                else
                {
                    objUser = null;
                }
            }
            catch(Exception ex)
            {
                clsLog.logError("User", "GetUserbyLoginId", ex.Message);
            }
            return objUser;
        }

        public static List<User> GetClientUsers(long client_id, App_Utils.jQueryDataTableParamModel param, out int totalRecord)
        {
            List<User> objUser = new List<User>();
            totalRecord = 0;
            try
            {
                clsLog.logMsg("User", "GetClientUsers", "Started");

                DAL.ithubEntities db = new ithubEntities();
                var query = (from q in db.tblUsers
                                          where q.client_id == client_id
                                              && q.is_visible == true
                                          select q);

                totalRecord = query.Count();

                // Sorting here

                query = query.OrderByDescending(m => m.create_date);



                List<tblUser> _objUser = query.Skip(param.iDisplayStart).Take(param.iDisplayLength).ToList();
                foreach (tblUser item in _objUser)
                {
                    objUser.Add((User)App_Utils.clsUtils.ConvertToObject(item, new User()));
                }
            }
            catch (Exception ex)
            {
                clsLog.logError("User", "GetClientUsers", ex.Message);
            }
            return objUser;
        }

        public static User SaveRecord(User objUser)
        {
            try
            {
                clsLog.logMsg("User", "SaveRecord", "Started");
                DAL.ithubEntities db = new ithubEntities();
                var query = (from q in db.tblUsers where q.user_id == objUser.user_id select q).SingleOrDefault();

                if (query != null)
                {
                    query = (tblUser)clsUtils.ConvertToObject(objUser, new tblUser());
                    db.SaveChanges();
                }
                else
                {
                    tblUser _objtblUser = new tblUser();
                    _objtblUser = (tblUser)clsUtils.ConvertToObject(objUser, new tblUser());
                    db.tblUsers.Add(_objtblUser);
                    db.SaveChanges();
                    objUser = (User)clsUtils.ConvertToObject(_objtblUser, objUser);
                }
                
            }
            catch (Exception ex)
            {
                objUser = null;
                clsLog.logError("User", "SaveRecord", ex.Message);
            }
            return objUser;
        }
    }
}
